package com.czk.pra;

import com.czk.ServerIps;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @Author:ChenZhangKun
 * @Date: 2023/7/17 11:46
 */
public class RoundRobinPra {
    private static  int pos=0;

   static final ReentrantLock lock=new ReentrantLock();

    public static void main(String[] args) {
        for (int i = 0; i < 15; i++) {
            System.out.println(getServerIp());
        }
    }

    public static String getServerIp(){
        try {
            lock.lock();
            if (pos>= ServerIps.LIST.size()){
                pos=0;
            }
            String ip = ServerIps.LIST.get(pos);
            pos++;
            return ip;
        }finally {
            lock.unlock();
        }

    }


}
